package com.mycp.privacy

import com.android.build.gradle.AppExtension
import com.didiglobal.booster.gradle.getAndroid
import com.mycp.privacy.transform.AnnotationRegisterTransform
import com.mycp.privacy.transform.PrivacyRegisterTransform
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.lang.Exception
/**
 * @author: liujie
 * @date: 2022/8/31 上午10:18
 * @description: 隐私整改插件
 */
class PrivacyPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        when {
            project.plugins.hasPlugin("com.android.application") ||
                    project.plugins.hasPlugin("com.android.dynamic-feature") -> {
                try {
                    val pluginBuildConfig = Class.forName("com.mycp.depend.plugin.BuildConfig")
                    val isOpenPrivacyPlugin = pluginBuildConfig.getField("isOpenPrivacyPlugin")
                    println("是否开启了隐私整改插件>>>>>>>"+isOpenPrivacyPlugin.getBoolean(null))
                    if(isOpenPrivacyPlugin.getBoolean(null)){
                        println("开始注册插件")
                        project.getAndroid<AppExtension>().let { androidExt ->
                            println("成功注册插件")
                            println("*****************************************************************************************\n" +
                                    "*****************************************************************************************\n" +
                                    "*****************************************************************************************\n" +
                                    " ▄       ▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄ \n" +
                                    "▐░▌     ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌\n" +
                                    " ▐░▌   ▐░▌  ▀▀▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌\n" +
                                    "  ▐░▌ ▐░▌            ▐░▌▐░▌       ▐░▌▐░▌       ▐░▌▐░▌       ▐░▌\n" +
                                    "   ▐░▐░▌    ▄▄▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌\n" +
                                    "    ▐░▌    ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌\n" +
                                    "   ▐░▌░▌   ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀█░█▀▀ ▐░█▀▀▀▀▀▀▀▀▀ \n" +
                                    "  ▐░▌ ▐░▌  ▐░▌          ▐░▌          ▐░▌     ▐░▌  ▐░▌          \n" +
                                    " ▐░▌   ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌          ▐░▌      ▐░▌ ▐░▌          \n" +
                                    "▐░▌     ▐░▌▐░░░░░░░░░░░▌▐░▌          ▐░▌       ▐░▌▐░▌          \n" +
                                    " ▀       ▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀            ▀         ▀  ▀           \n" +
                                    "*****************************************************************************************\n" +
                                    "*****************************************************************************************\n" +
                                    "*****************************************************************************************\n")
                            androidExt.registerTransform(
                                AnnotationRegisterTransform(project)
                            )
                            androidExt.registerTransform(
                                PrivacyRegisterTransform(project)
                            )
                        }
                    }
                } catch (e: Exception) {
                }
            }
        }
    }
}